package jp.co.technotree.common.dao;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.HashMap;

import jp.co.technotree.common.util.DBGLog;


public class MethodMapFactory {
	private static String CLASSNAME=MethodMapFactory.class.getName();
	
	public static final int TYPE_INT=1;
	public static final int TYPE_FLOAT=2;
	public static final int TYPE_STRING=3;
	public static final int TYPE_INTARR=4;
	public static final int TYPE_FLOATARR=5;
	public static final int TYPE_STRINGARR=6;
	public static final int TYPE_FILE=7;
	public static final int TYPE_BIGDECIMAL=8;
	public static final int TYPE_SQLDATE=9;
	public static final int TYPE_SQLTIME=10;
	public static final int TYPE_SQLTIMESTAMP=11;
	public static final int TYPE_LONG=12;
	public static final int TYPE_BIGDECIMALARR=13;

	
	
	private static MethodMapFactory instance = new MethodMapFactory();
	
	private MethodMapFactory(){
		BigDecimal a=null;
		String METHODNAME = "MethodMapFactory";
		DBGLog.fwdebug(CLASSNAME,METHODNAME,"START");
		DBGLog.fwdebug(CLASSNAME,METHODNAME,"END");
		
	}
	
	public static MethodMapFactory getInstance(){
		return instance;
	}
	
	public HashMap getMethodMap(Class formBeanClass){
		String METHODNAME = "getMethodMap";
		DBGLog.fwdebug(CLASSNAME,METHODNAME,"START");

		Method[] methods = formBeanClass.getMethods();
		HashMap<String, MethodMap> map = new HashMap<String, MethodMap>();
		MethodMap methodMap = null;
		int i;
		
		DBGLog.fwdebug(CLASSNAME,METHODNAME,"Class:"+formBeanClass.getName());
		
		for ( i=0; i<methods.length; i++ ){
			Method method = methods[i];
			Class[] params = method.getParameterTypes();
			String methodName = method.getName();
			String paramName = null;
			int paramType = 0;
			
			if ( methodName != null && methodName.indexOf("set") == 0 ){
				// setterのメソッド名を取得
				if ( params.length == 1 ){
					// パラメータ数が１つの物
					paramName = params[0].getName();
					if ( paramName != null ) paramName = paramName.toLowerCase();
					paramType = getType(paramName);
					
					if ( params[0].isArray() ){
						// パラメータが配列の場合
						if ( paramType == MethodMapFactory.TYPE_INT ){
							paramType = MethodMapFactory.TYPE_INTARR;
						}else if ( paramType == MethodMapFactory.TYPE_STRING ){
							paramType = MethodMapFactory.TYPE_STRINGARR;
						}else if ( paramType == MethodMapFactory.TYPE_BIGDECIMAL ){
							paramType = MethodMapFactory.TYPE_BIGDECIMALARR;
						}
					}
					// ハッシュにメソッドＭＡＰをセット
					DBGLog.fwdebug(CLASSNAME,METHODNAME,"add:["+methodName+":"+paramType+"]");
					methodMap = new MethodMap(method,paramType);
					map.put(methodName.toLowerCase(),methodMap);
				}
			}
		}
		DBGLog.fwdebug(CLASSNAME,METHODNAME,"END");
		
		return map;
	}
	
	public static int getType(String className){
		String lowerClassName = null;
		int result = -1;
		
		if ( className != null ){
			lowerClassName = className.toLowerCase();
			
			DBGLog.fwdebug(CLASSNAME,"CLASS",lowerClassName);
			if ( lowerClassName.equals("int") ){
				result = TYPE_INT;
			}else if ( lowerClassName.indexOf(".string") != -1 ){
				result = TYPE_STRING;
//			}else if ( lowerClassName.equals(UploadFileBean.class.getName().toLowerCase()) ){
//				result = TYPE_FILE;
			}else if ( lowerClassName.equals("[i") ){
				result = TYPE_INTARR;
			}else if ( lowerClassName.equals("float") ){
				result = TYPE_FLOAT;
			}else if ( lowerClassName.equals("[f") ){
				result = TYPE_INTARR;
			}else if ( lowerClassName.equals("java.math.bigdecimal") ){
				result = TYPE_BIGDECIMAL;
			}else if ( lowerClassName.equals("[ljava.math.bigdecimal;") ){
				result = TYPE_BIGDECIMALARR;
			}else if ( lowerClassName.equals("java.sql.date") ){
				result = TYPE_SQLDATE;
			}else if ( lowerClassName.equals("java.sql.time") ){
				result = TYPE_SQLTIME;
			}else if ( lowerClassName.equals("java.sql.timestamp") ){
				result = TYPE_SQLTIMESTAMP;
			}else if ( lowerClassName.equals("long") ){
				result = TYPE_LONG;
			}
		}
		return result;
	}
}
